package com.yjt.fresh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.activity.AtyGetCoupon;
import com.yjt.fresh.activity.AtyStoreDetail;
import com.yjt.fresh.adpater.AdpRvAtyMyCoupon;
import com.yjt.fresh.api.ApiGetCoupons;
import com.yjt.fresh.base.BaseSimpleFragment;
import com.yjt.fresh.bean.BeanCoupon;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.nfc.tech.MifareUltralight.PAGE_SIZE;

/**
 *
 * Created by mcc on 2018/6/17.
 */

public class FrgMyCouponNotYet extends BaseSimpleFragment {
    private static final int REQUEST_CODE = 1000;
    @BindView(R.id.rv_app_aty_mycoupon_frg_noteYet)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyMyCouponFrgNotYet)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.tvAppAtyMyCouponFrgNotYetToCouponCenter)
    TextView mTvToCouponCenter;
    private boolean isLoadMore;
    private int mNextRequestPage = 1;
    private AdpRvAtyMyCoupon mAdpMyCoupon;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_mycoupon_frg_notyet;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        ButterKnife.bind(this,view);
    }

    @Override
    protected void initEvents() {
        initRV();
        getData();
        initClick();
    }

    private void initClick() {

        mTvToCouponCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AtyGetCoupon.class);
                startActivityForResult(intent,REQUEST_CODE);
            }
        });
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });

        mAdpMyCoupon.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        },mRv);

        mAdpMyCoupon.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tvAppAtyMyCouponStatus){
                    Intent intent = new Intent(mContext, AtyStoreDetail.class);
                    intent.putExtra(MyConstants.STORE_ID,mAdpMyCoupon.getItem(position).getOWNER());
                    intent.putExtra(MyConstants.STORE_NAME,mAdpMyCoupon.getItem(position).getSTORE_NAME());
                    intent.putExtra(MyConstants.STORE_LOGO,mAdpMyCoupon.getItem(position).getLOGO_IMG());
                    intent.putExtra(MyConstants.STORE_PHONE,mAdpMyCoupon.getItem(position).getTEL());
                    intent.putExtra(MyConstants.STORE_LOCATION,mAdpMyCoupon.getItem(position).getLATITUDE_LONGITUDE());
                    startActivity(intent);
                }
            }
        });
    }

    HttpOnNextListener<BeanCoupon> mCouponListener = new HttpOnNextListener<BeanCoupon>() {
        @Override
        public void onNext(BeanCoupon beanCoupon) {
            mAdpMyCoupon.setEmptyView(R.layout.view_empty);
            if (isLoadMore) {
                setData(false, beanCoupon.getList());
            } else {
                setData(true, beanCoupon.getList());
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void setData(boolean isRefresh, List<BeanCoupon.ListBean> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpMyCoupon.getData().clear();
            mAdpMyCoupon.addData(data);
            mAdpMyCoupon.setEnableLoadMore(false);
        } else {
            if (size > 0) {
                mAdpMyCoupon.addData(data);
                mAdpMyCoupon.loadMoreComplete();
            }else {
                mAdpMyCoupon.loadMoreEnd(true);
            }
        }
    }
    private void getData() {
        //TODO 优惠券的数量
        ApiGetCoupons apiGetCoupons = new ApiGetCoupons(mCouponListener,mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        map.put("currentPage",mNextRequestPage);
        map.put("STATUS","1");
        apiGetCoupons.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetCoupons);
    }

    private void initRV() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanCoupon.ListBean> list = new ArrayList<>();
        mAdpMyCoupon = new AdpRvAtyMyCoupon(R.layout.app_recycle_item_aty_mycoupon_frg,list);
        mAdpMyCoupon.setStatus(MyConstants.NOT_YET);
        mRv.setAdapter(mAdpMyCoupon);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE){
            mNextRequestPage = 1;
            isLoadMore = false;
            getData();
        }
    }
}
